function useClickOutside(ref, callback, onMouseDown) {
  const clickOutsideHandler = e => {
    onMouseDown && onMouseDown(e);
    if (ref instanceof Array) {
      let inSide = false;
      ref.forEach(item => {
        if (item.contains && item.contains(e.target)) {
          inSide = true;
        }
        if (item.forEach) {
          item.forEach(subItem => {
            if (subItem.contains && subItem.contains(e.target)) {
              inSide = true;
            }
          });
        }
      });
      if (!inSide) {
        callback && callback();
      }
    } else {
      if (!ref.contains(e.target)) {
        callback && callback();
      }
    }
  };
  const removeEventListener = () => {
    document.removeEventListener('mousedown', clickOutsideHandler);
  };
  document.addEventListener('mousedown', clickOutsideHandler);
  return removeEventListener;
}

export { useClickOutside as default };
